"use strict";

var AppsCollection = Backbone.Collection.extend( {
/*
  active        : false
, currentApp    : null
,*/ 
  appList       : $( '#page-list' )
, buttonList    : $( '#dot-list' )
, nextButton    : $( '#page-switcher-end' )
, prevButton    : $( '#page-switcher-start' )
, model         : App

, initialize    : function( models, options ) {
   
    this.bookmarkStorage = new BookmarkStoreAdapter( this, options.bookmarkTreeNodeId );
    
    console.log( 'AppsCollection.initialize', this );

    this.appList.addClass( 'loader' );

  }
/*
, activate        : function() {
  
    var that = this;
    
    if ( !this.isActive() ) {
      
      this.currentApp = this.at( 0 );
      
      this.appList.append( this.currentApp.activate() );
      
      //TODO: For testing
      this.appList.append( this.at( 1 ).activate() );
      this.appList.append( this.at( 2 ).activate() );
      
      
      this.currentApp.show();
      
      this.appList.scrollTo( this.currentApp.frame(), {
        duration : 1000
      } );
      
      this.forEach( function( element, index, list ) {
        that.buttonList.append( element.button() );
      } );
      
      this.nextButton.click( function( evt ) { that.next(); } );
      this.prevButton.click( function( evt ) { that.prev(); } );
      
    }
  
    this.active = true;
  
    //TODO: attach prev and next buttons if there are so many applications and show them, else hide prev or next or both
    if ( this.length > 1 ) this.nextButton.show(); 

  }
, initUI        : function() {
    
    console.log( 'initUI' )
    
    this.appList.removeClass( 'loader' );
  }
, isActive      : function() {
    return this.active;
  }
, hasNext       : function() {
    var i = this.at( this.indexOf( this.currentApp ) + 1 );
    return ( undefined === i || i < 0 );
  }
, hasPrev       : function() {
    var i = this.at( this.indexOf( this.currentApp ) - 1 );
    return ( undefined === i || i < 1 );
  }
, next          : function() {
    this.show( this.hasNext() ? this.at( this.indexOf( this.currentApp ) + 1 ) : this.at( 1 ) );
  }
, prev          : function() {
    this.show( this.hasPrev() ? this.at( this.indexOf( this.currentApp ) - 1 ) : this.at( this.length - 1 ) );
  }          
, show          : function( app ) {
    
    console.log( 'show: ', app.get( 'title' ) );

    console.log( 'app == currentApp', app == this.currentApp )
    
    if( app == this.currentApp ) {
      console.log( 'do nothing' );
    } else {
      //TODO: calculate where current and new app are in index to know where to scroll to...
      
      console.log( 'app.isActive()', app.isActive() );
      
      if ( !app.isActive() ) this.appList.append( app.activate() );
      
      this.currentApp.hide();
      this.currentApp = app;
      
      console.log( app == this.currentApp )
      
      this.currentApp.setWidth( this.appList.width() / this.length );
      this.currentApp.show();
      this.appList.scrollTo( this.currentApp.frame(), {
        duration : 1000
      } );
    
    }
  
  
  }*/
 
} );